import smap.archiver.client
import time
import scipy.interpolate
import csv
import calendar

#change the name and location of the output file
f1 = open('/home/cbe/singletest.csv', 'wb')
testsout = csv.writer(f1,dialect='excel') 

#input stream ids
c = smap.archiver.client.SmapClient(base='http://new.openbms.org/backend', key='SA2nYWuHrJxmPNK96pdLKhnSSYQSPdALkvnA', private=True)

#insert local times
start_time = int(calendar.timegm(time.strptime('2011-08-28 19:00:00', '%Y-%m-%d %H:%M:%S')))
end_time = int(calendar.timegm(time.strptime('2012-01-10 08:00:00', '%Y-%m-%d %H:%M:%S')))  
#new_time = []
#interpolated = []
#interval = 600 #number of seconds for interval
#x = start_time + interval
#while x < end_time-interval: #shrink total time interval by one interval length on each side to account for missing data
#    x = x + interval 
#    new_time.append(x*1000)
newdata = c.data('uuid = \'5885fe5d-71f9-527c-92e3-ccd705cc15cb\'',start_time, end_time)
#diag = c.query('select distinct uuid where Metadata/Extra/DeviceName = \'Plenum temp\'')
#print diag


#rint max(new_time)
#rint int(end_time*1000)
#axi = 4000000000000000
#for row in newdata[1]:
#   maxnew = max(row[:,0])
#   if maxnew < maxi:
#       maxi = maxnew 
#    f_data = scipy.interpolate.interp1d(row[:,0], row[:,1])  
#    interpolated.append(f_data(new_time))
#transposed = zip(*interpolated)
#rint int(maxi)
#print newdata
names = []
for uuid in newdata[0]:
    names.append(c.query('select distinct Metadata/Extra/GridLocation where uuid = \'%s\'' % uuid))
row = ['uuids']
row.extend(newdata[0])
testsout.writerow(row)
row = ['GridLocation']
row.extend(names)
testsout.writerow(row)
#print newdata[1][0]
for row in newdata[1][0]:
    roww = [time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(row[0]/1000))]
    roww.extend([row[1]])
    testsout.writerow(roww)    

f1.close()

